/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.hugegraph.pd.watch;

import static org.apache.hugegraph.pd.common.HgAssert.isArgumentNotNull;

import org.apache.hugegraph.pd.grpc.Metapb;
import org.apache.hugegraph.pd.grpc.watch.WatchChangeType;
import org.apache.hugegraph.pd.grpc.watch.WatchResponse;
import org.apache.hugegraph.pd.grpc.watch.WatchType;

public class ShardGroupChangeSubject extends AbstractWatchSubject {

    protected ShardGroupChangeSubject() {
        super(WatchType.WATCH_TYPE_SHARD_GROUP_CHANGE);
    }

    @Override
    String toNoticeString(WatchResponse res) {
        StringBuilder sb = new StringBuilder();
        sb.append("shard group:")
          .append(res.getShardGroupResponse().getShardGroup().toString().replace("\n", " "));
        return sb.toString();
    }

    public void notifyWatcher(WatchChangeType changeType, int groupId,
                              Metapb.ShardGroup shardGroup) {
        isArgumentNotNull(changeType, "changeType");

        super.notifyWatcher(builder -> {
            builder.setShardGroupResponse(
                    builder.getShardGroupResponseBuilder().clear()
                           .setShardGroupId(groupId)
                           .setType(changeType)
                           .setShardGroup(shardGroup)
                           .build()
            );
        });
    }
}
